using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CTextMessage : CBaseDynamic, IComparable<CTextMessage>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CTextMessage(CTextMessage original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _date = original.Date;
            _body = original.Body;
            _fromTo = original.FromTo;
            _direction = original.Direction;
            _textLen = original.TextLen;
            _imagesLen = original.ImagesLen;
            _contactName = original.ContactName;
            _type = original.Type;
            _addresses = original.Addresses;
            _isMms = original.IsMms;
            _text = original.Text;
            _address = original.Address;
            _serviceCenter = original.ServiceCenter;
            _subject = original.Subject;
            _status = original.Status;
            _read = original.Read;
            _locked = original.Locked;
            _dateSent = original.DateSent;
            _dateRead = original.DateRead;
            _otherProtoGz = original.OtherProtoGz;
            _images = original.Images;
        }

        //Protected (Datareader/Dataset)
        protected CTextMessage(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CTextMessage(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _messageId = int.MinValue;
            _isMms = false;
            _body = string.Empty;
            _text = string.Empty;
            _contactName = string.Empty;
            _address = string.Empty;
            _addresses = string.Empty;
            _serviceCenter = string.Empty;
            _subject = string.Empty;
            _type = int.MinValue;
            _status = int.MinValue;
            _read = false;
            _locked = false;
            _date = DateTime.MinValue;
            _dateSent = DateTime.MinValue;
            _dateRead = DateTime.MinValue;
            _otherProtoGz = new byte[]{};
            _images = new byte[]{};
        }
        #endregion

        #region Members
        protected long _messageId;
        protected DateTime _date;
        protected string _body;
        protected string _fromTo;
        protected string _direction;
        protected long _textLen;
        protected long _imagesLen;
        protected string _contactName;
        protected int _type;
        protected string _addresses;
        protected bool _isMms;
        protected string _text;
        protected string _address;
        protected string _serviceCenter;
        protected string _subject;
        protected int _status;
        protected bool _read;
        protected bool _locked;
        protected DateTime _dateSent;
        protected DateTime _dateRead;
        protected byte[] _otherProtoGz;
        protected byte[] _images;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public long MessageId    {   get { return _messageId;   } }

        //Table Columns (Read/Write)
        public bool IsMms    {   get { return _isMms; } set { _isMms = value; }    }
        public string Body    {   get { return _body; } set { _body = value; }    }
        public string Text    {   get { return _text; } set { _text = value; }    }
        public string ContactName    {   get { return _contactName; } set { _contactName = value; }    }
        public string Address    {   get { return _address; } set { _address = value; }    }
        public string Addresses    {   get { return _addresses; } set { _addresses = value; }    }
        public string ServiceCenter    {   get { return _serviceCenter; } set { _serviceCenter = value; }    }
        public string Subject    {   get { return _subject; } set { _subject = value; }    }
        public int Type    {   get { return _type; } set { _type = value; }    }
        public int Status    {   get { return _status; } set { _status = value; }    }
        public bool Read    {   get { return _read; } set { _read = value; }    }
        public bool Locked    {   get { return _locked; } set { _locked = value; }    }
        public DateTime Date    {   get { return _date; } set { _date = value; }    }
        public DateTime DateSent    {   get { return _dateSent; } set { _dateSent = value; }    }
        public DateTime DateRead    {   get { return _dateRead; } set { _dateRead = value; }    }
        public byte[] OtherProtoGz    {   get { return _otherProtoGz; } set { _otherProtoGz = value; }    }
        public byte[] Images    {   get { return _images; } set { _images = value; }    }

        //View Columns (ReadOnly)
        public string FromTo    {   get { return _fromTo;   } }
        public string Direction    {   get { return _direction;   } }
        public long TextLen    {   get { return _textLen;   } }
        public long ImagesLen    {   get { return _imagesLen;   } }

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "TextMessage";
        public const string VIEW_NAME       = "vwTextMessages";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "[Date]";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string ViewName {    get {   return VIEW_NAME;    }    }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CTextMessage other) {   return this.MessageId.CompareTo(other.MessageId) ;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "[Date] ASC";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _messageId; }
              set { _messageId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CTextMessage(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CTextMessage(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CTextMessageList();               }
        protected override IList MakeList(int capacity)     {   return new CTextMessageList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _messageId = CAdoData.GetLong(dr, "MessageID");
            _date = CAdoData.GetDate(dr, "Date");
            _body = CAdoData.GetStr(dr, "Body");
            _fromTo = CAdoData.GetStr(dr, "FromTo");
            _direction = CAdoData.GetStr(dr, "Direction");
            _textLen = CAdoData.GetLong(dr, "TextLen");
            _imagesLen = CAdoData.GetLong(dr, "ImagesLen");
            _contactName = CAdoData.GetStr(dr, "ContactName");
            _type = CAdoData.GetInt(dr, "Type");
            _addresses = CAdoData.GetStr(dr, "Addresses");
            _isMms = CAdoData.GetBool(dr, "IsMms");
            _text = CAdoData.GetStr(dr, "Text");
            _address = CAdoData.GetStr(dr, "Address");
            _serviceCenter = CAdoData.GetStr(dr, "ServiceCenter");
            _subject = CAdoData.GetStr(dr, "Subject");
            _status = CAdoData.GetInt(dr, "Status");
            _read = CAdoData.GetBool(dr, "Read");
            _locked = CAdoData.GetBool(dr, "Locked");
            _dateSent = CAdoData.GetDate(dr, "DateSent");
            _dateRead = CAdoData.GetDate(dr, "DateRead");
            _otherProtoGz = CAdoData.GetBytes(dr, "OtherProtoGz");
            _images = CAdoData.GetBytes(dr, "Images");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _messageId = CAdoData.GetLong(dr, "MessageID");
            _date = CAdoData.GetDate(dr, "Date");
            _body = CAdoData.GetStr(dr, "Body");
            _fromTo = CAdoData.GetStr(dr, "FromTo");
            _direction = CAdoData.GetStr(dr, "Direction");
            _textLen = CAdoData.GetLong(dr, "TextLen");
            _imagesLen = CAdoData.GetLong(dr, "ImagesLen");
            _contactName = CAdoData.GetStr(dr, "ContactName");
            _type = CAdoData.GetInt(dr, "Type");
            _addresses = CAdoData.GetStr(dr, "Addresses");
            _isMms = CAdoData.GetBool(dr, "IsMms");
            _text = CAdoData.GetStr(dr, "Text");
            _address = CAdoData.GetStr(dr, "Address");
            _serviceCenter = CAdoData.GetStr(dr, "ServiceCenter");
            _subject = CAdoData.GetStr(dr, "Subject");
            _status = CAdoData.GetInt(dr, "Status");
            _read = CAdoData.GetBool(dr, "Read");
            _locked = CAdoData.GetBool(dr, "Locked");
            _dateSent = CAdoData.GetDate(dr, "DateSent");
            _dateRead = CAdoData.GetDate(dr, "DateRead");
            _otherProtoGz = CAdoData.GetBytes(dr, "OtherProtoGz");
            _images = CAdoData.GetBytes(dr, "Images");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("MessageId", CAdoData.NullVal(_messageId));
            data.Add("IsMms", CAdoData.NullVal(_isMms));
            data.Add("Body", CAdoData.NullVal(_body));
            data.Add("Text", CAdoData.NullVal(_text));
            data.Add("ContactName", CAdoData.NullVal(_contactName));
            data.Add("Address", CAdoData.NullVal(_address));
            data.Add("Addresses", CAdoData.NullVal(_addresses));
            data.Add("ServiceCenter", CAdoData.NullVal(_serviceCenter));
            data.Add("Subject", CAdoData.NullVal(_subject));
            data.Add("Type", CAdoData.NullVal(_type));
            data.Add("Status", CAdoData.NullVal(_status));
            data.Add("Read", CAdoData.NullVal(_read));
            data.Add("Locked", CAdoData.NullVal(_locked));
            data.Add("Date", CAdoData.NullVal(_date));
            data.Add("DateSent", CAdoData.NullVal(_dateSent));
            data.Add("DateRead", CAdoData.NullVal(_dateRead));
            data.Add("OtherProtoGz", CAdoData.NullVal(_otherProtoGz));
            data.Add("Images", CAdoData.NullVal(_images));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CTextMessageList SelectAll()                                                          {    return (CTextMessageList)base.SelectAll();                              }
        public new CTextMessageList SelectAll(string orderBy)                                            {    return (CTextMessageList)base.SelectAll(orderBy);                       }
        public new CTextMessageList SelectWhere(string colName, ESign sign, object colValue)             {    return (CTextMessageList)base.SelectWhere(colName, sign, colValue);     }
        public new CTextMessageList SelectWhere(CCriteria where)                                         {    return (CTextMessageList)base.SelectWhere(where);                       }
        public new CTextMessageList SelectWhere(CCriteriaList where)                                     {    return (CTextMessageList)base.SelectWhere(where);                       }
        public new CTextMessageList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CTextMessageList)base.SelectWhere(where, tableOrJoin);          }
        public new CTextMessageList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CTextMessageList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CTextMessageList SelectWhere(string unsafeWhereClause)                                {    return (CTextMessageList)base.SelectWhere(unsafeWhereClause);           }
        public     CTextMessageList SelectById(int message)                              {    return (CTextMessageList)base.SelectById(message);       }
        public     CTextMessageList SelectByIds(List<int> ids)                        {    return (CTextMessageList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CTextMessageList SelectAll(  CPagingInfo pi)                                              {    return (CTextMessageList)base.SelectAll(  pi);                              }
        public new CTextMessageList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CTextMessageList)base.SelectWhere(pi, name, sign, value);           }
        public new CTextMessageList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CTextMessageList)base.SelectWhere(pi, criteria);                    }
        public new CTextMessageList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CTextMessageList)base.SelectWhere(pi, criteria);                    }
        public new CTextMessageList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CTextMessageList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CTextMessageList SelectByIds(CPagingInfo pi, List<int> ids)            {    return (CTextMessageList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CTextMessageList SelectAll(                                                                                           IDbTransaction tx)  {    return (CTextMessageList)base.SelectAll(tx);                                                     }
        public new CTextMessageList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CTextMessageList)base.SelectAll(orderBy,                                         tx);    }
        public new CTextMessageList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CTextMessageList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CTextMessageList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(               criteria,                       tx);    }
        public new CTextMessageList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(               criteria,                       tx);    }
        public new CTextMessageList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CTextMessageList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CTextMessageList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CTextMessageList SelectById(int message,                                                              IDbTransaction tx)  {    return (CTextMessageList)base.SelectById(message,                         tx);    }
        public     CTextMessageList SelectByIds(List<int> ids,                                                        IDbTransaction tx)  {    return (CTextMessageList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CTextMessageList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName,             txOrNull);  }
        public new CTextMessageList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CTextMessageList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CTextMessageList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CTextMessageList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CTextMessageList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CTextMessageList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CTextMessageList MakeList(DataSet              ds) { return (CTextMessageList)base.MakeList(ds);        }
        protected new CTextMessageList MakeList(DataTable            dt) { return (CTextMessageList)base.MakeList(dt);        }
        protected new CTextMessageList MakeList(DataRowCollection  rows) { return (CTextMessageList)base.MakeList(rows);      }
        protected new CTextMessageList MakeList(IDataReader          dr) { return (CTextMessageList)base.MakeList(dr);        }
        protected new CTextMessageList MakeList(object           drOrDs) { return (CTextMessageList)base.MakeList(drOrDs);    }        
        protected new CTextMessageList MakeList(byte[]             gzip) { return (CTextMessageList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CTextMessageList SelectByIsMms(bool isMms)    {    return SelectWhere(new CCriteriaList("IsMms", isMms));    }
        public CTextMessageList SelectByContactName(string contactName)    {    return SelectWhere(new CCriteriaList("ContactName", contactName));    }
        public CTextMessageList SelectByAddress(string address)    {    return SelectWhere(new CCriteriaList("Address", address));    }
        public CTextMessageList SelectByAddresses(string addresses)    {    return SelectWhere(new CCriteriaList("Addresses", addresses));    }
        public CTextMessageList SelectBySubject(string subject)    {    return SelectWhere(new CCriteriaList("Subject", subject));    }
        public CTextMessageList SelectByType(int type)    {    return SelectWhere(new CCriteriaList("Type", type));    }
        public CTextMessageList SelectByStatus(int status)    {    return SelectWhere(new CCriteriaList("Status", status));    }
        public CTextMessageList SelectByRead(bool read)    {    return SelectWhere(new CCriteriaList("Read", read));    }
        public CTextMessageList SelectByLocked(bool locked)    {    return SelectWhere(new CCriteriaList("Locked", locked));    }
        public CTextMessageList SelectByDate(DateTime date)    {    return SelectWhere(new CCriteriaList("Date", date));    }

        //Paged
        public CTextMessageList SelectByIsMms(CPagingInfo pi, bool isMms)    {    return SelectWhere(pi, new CCriteriaList("IsMms", isMms));    }
        public CTextMessageList SelectByContactName(CPagingInfo pi, string contactName)    {    return SelectWhere(pi, new CCriteriaList("ContactName", contactName));    }
        public CTextMessageList SelectByAddress(CPagingInfo pi, string address)    {    return SelectWhere(pi, new CCriteriaList("Address", address));    }
        public CTextMessageList SelectByAddresses(CPagingInfo pi, string addresses)    {    return SelectWhere(pi, new CCriteriaList("Addresses", addresses));    }
        public CTextMessageList SelectBySubject(CPagingInfo pi, string subject)    {    return SelectWhere(pi, new CCriteriaList("Subject", subject));    }
        public CTextMessageList SelectByType(CPagingInfo pi, int type)    {    return SelectWhere(pi, new CCriteriaList("Type", type));    }
        public CTextMessageList SelectByStatus(CPagingInfo pi, int status)    {    return SelectWhere(pi, new CCriteriaList("Status", status));    }
        public CTextMessageList SelectByRead(CPagingInfo pi, bool read)    {    return SelectWhere(pi, new CCriteriaList("Read", read));    }
        public CTextMessageList SelectByLocked(CPagingInfo pi, bool locked)    {    return SelectWhere(pi, new CCriteriaList("Locked", locked));    }
        public CTextMessageList SelectByDate(CPagingInfo pi, DateTime date)    {    return SelectWhere(pi, new CCriteriaList("Date", date));    }

        //Count
        public int SelectCountByIsMms(bool isMms)   {   return SelectCount(new CCriteriaList("IsMms", isMms));     }
        public int SelectCountByContactName(string contactName)   {   return SelectCount(new CCriteriaList("ContactName", contactName));     }
        public int SelectCountByAddress(string address)   {   return SelectCount(new CCriteriaList("Address", address));     }
        public int SelectCountByAddresses(string addresses)   {   return SelectCount(new CCriteriaList("Addresses", addresses));     }
        public int SelectCountBySubject(string subject)   {   return SelectCount(new CCriteriaList("Subject", subject));     }
        public int SelectCountByType(int type)   {   return SelectCount(new CCriteriaList("Type", type));     }
        public int SelectCountByStatus(int status)   {   return SelectCount(new CCriteriaList("Status", status));     }
        public int SelectCountByRead(bool read)   {   return SelectCount(new CCriteriaList("Read", read));     }
        public int SelectCountByLocked(bool locked)   {   return SelectCount(new CCriteriaList("Locked", locked));     }
        public int SelectCountByDate(DateTime date)   {   return SelectCount(new CCriteriaList("Date", date));     }

        //Transactional
        public CTextMessageList SelectByIsMms(bool isMms, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("IsMms", isMms), tx);    }
        public CTextMessageList SelectByContactName(string contactName, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ContactName", contactName), tx);    }
        public CTextMessageList SelectByAddress(string address, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Address", address), tx);    }
        public CTextMessageList SelectByAddresses(string addresses, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Addresses", addresses), tx);    }
        public CTextMessageList SelectBySubject(string subject, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Subject", subject), tx);    }
        public CTextMessageList SelectByType(int type, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Type", type), tx);    }
        public CTextMessageList SelectByStatus(int status, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Status", status), tx);    }
        public CTextMessageList SelectByRead(bool read, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Read", read), tx);    }
        public CTextMessageList SelectByLocked(bool locked, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Locked", locked), tx);    }
        public CTextMessageList SelectByDate(DateTime date, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("Date", date), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "MessageId", this.MessageId);
            Store(w, "IsMms", this.IsMms);
            Store(w, "Body", this.Body);
            Store(w, "Text", this.Text);
            Store(w, "ContactName", this.ContactName);
            Store(w, "Address", this.Address);
            Store(w, "Addresses", this.Addresses);
            Store(w, "ServiceCenter", this.ServiceCenter);
            Store(w, "Subject", this.Subject);
            Store(w, "Type", this.Type);
            Store(w, "Status", this.Status);
            Store(w, "Read", this.Read);
            Store(w, "Locked", this.Locked);
            Store(w, "Date", this.Date);
            Store(w, "DateSent", this.DateSent);
            Store(w, "DateRead", this.DateRead);
            Store(w, "OtherProtoGz", this.OtherProtoGz);
            Store(w, "Images", this.Images);
        }
        #endregion

    }
}
